// pages/navigation/old/work/work.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    calendarList: [],
    fairList: [],
    color:['olive','cyan','mauve','blue','yellow','pink'],
    isConfirm: false,
    // 时间选择
    currentDate: new Date().getTime(),
    minDate: new Date(2021, 0, 1).getTime(),
    // 三个月后
    maxDate: new Date(new Date().setMonth(new Date().getMonth() + 3)).getTime(),
  },
  /**
   * 点击确定月份
   */
  onConfirm(e) {
    const year = parseInt(this.getResult(this.data.currentDate).split("/")[0]);
    const month = parseInt(this.getResult(this.data.currentDate).split("/")[1]);
    console.log('year',year);
    console.log('month',month);
    wx.showLoading({
      title: '正在查询',
    })
    //获取calendarList
    const db = wx.cloud.database({});
    const _ = db.command;
    var date1 = new Date(year, month-1, 1);
    var date2 = new Date(year, month, 1);
    console.log('data1',date1);
    console.log('date2',date2);
    db.collection('job_info')
    .orderBy('launchdate','asc')
    .where({
      launchdate: _.gte(date1).and(_.lt(date2))
    })
    .get()
    .then(res => {
      console.log('指定月的所有招聘信息',res.data);
      var list = res.data;
      var calendarList = this.getList(list);
      this.setData({
        calendarList:calendarList
      })
      wx.hideLoading();
    })
    .catch(err => {
      console.log('err',err);
    }) 
  },
  nav: function(e){
    wx.navigateTo({
      url: 'detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 时间戳转为 年-月
   */
  getResult(time) {
    const date = new Date(time);
    return `${date.getFullYear()}/${date.getMonth() + 1}`;
  },
  /**
   * 监听时间选择
   */
  onInput(event) {
    this.setData({
      currentDate: event.detail
    });
  },
  getDate: function (date) {
    var y= date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '年' + m + '月' + d + '日';
  },
  getList:function(list){
    var fairList = [];
        var lauchdate = '';
        for(var i = 0; i <list.length; i++){
          list[i].launchdate = this.getDate(list[i].launchdate);
          if(list[i].launchdate == lauchdate){
            fairList[fairList.length-1].fairs.push(list[i]);
          }else{
            var item = {launchdate:list[i].launchdate,fairs:[]};
            item.fairs.push(list[i]);
            fairList.push(item);
          }
        }
        for(var i = 0; i < fairList.length; i++){
          fairList[i].color = this.data.color[i%6];
        }
        console.log('成功获取招聘会信息',fairList);
        return fairList;
  },
  /**
   * tab 切换事件
   */
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
    })
    // 判断当前页已经加载过了，就不再首次加载了
    if (e.currentTarget.dataset.id == 0 && this.data.calendarList.length != 0) {
      return;
    } else if (e.currentTarget.dataset.id == 1 && this.data.fairList.length != 0) {
      return;
    }
    // 请求标签所在的新闻
    wx.showLoading({
      title: '正在加载',
    })
    console.log('e',e.currentTarget.dataset.id);
    const year = this.getResult(this.data.currentDate).split("/")[0]
    const month = this.getResult(this.data.currentDate).split("/")[1]
    if (e.currentTarget.dataset.id == 0) {
      this.setData({
        calendarList:[],
        isConfirm: false,
      })
      wx.hideLoading();
    } else if (e.currentTarget.dataset.id == 1) {
      wx.cloud.database().collection('job_info')
      .orderBy('launchdate','asc')
      .get()
      .then(res => {
        var list = res.data;
        var fairList = this.getList(list);
        this.setData({
          fairList:fairList
        })
        wx.hideLoading();
      })
      .then(err => {
        console.log('未获取招聘会信息',err);
      })
    }

  }
})